# 管理员控制器
from flask import Blueprint, request, jsonify
from service.admin import AdminService
from common.response import APIResponse

res = APIResponse()

# 创建蓝图
admin_bp = Blueprint('admin', __name__, url_prefix='/admins')

@admin_bp.route('/list', methods=['GET'])
def get_admins():
    # 获取单个管理员或所有管理员
    admin_id = request.args.get('id')
    if admin_id:
        return AdminService.get_admin_by_id(admin_id)
    else:
        return AdminService.get_all_admins(request.args)

@admin_bp.route('/add', methods=['POST'])
def create_admin():
    # 创建新管理员
    data = request.get_json() or {}
    if not data:
        return res.validate_error('请提供管理员信息')
    return AdminService.create_admin(data)

@admin_bp.route('/update', methods=['POST'])
def update_admin():
    # 更新管理员
    data = request.get_json() or {}
    if not data or 'id' not in data:
        return res.validate_error('请提供管理员ID')
    admin_id = data.pop('id')
    return AdminService.update_admin(admin_id, data)

@admin_bp.route('/delete', methods=['POST'])
def delete_admin():
    # 删除管理员
    data = request.get_json() or {}
    if not data or 'id' not in data:
        return res.validate_error('请提供管理员ID')
    return AdminService.delete_admin(data['id'])

@admin_bp.route('/login', methods=['POST'])
def admin_login():
    # 管理员登录
    data = request.get_json() or {}
    if not data or 'phone' not in data or 'password' not in data:
        return res.validate_error('请提供手机号和密码')
    return AdminService.admin_login(data)